package com.ybbase.framework.advice;

import com.ybbase.framework.base.model.ErrorInfo;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常类
 *
 * @author chengang
 * @date 2019/8/1 9:45
 * @since 4.0
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    /**
     * @param req http请求
     * @param e   异常
     * @return ErrorInfo<String>
     * @author chengang
     * @date 2019/9/21 5:10
     * @since 4.0
     */
    @ExceptionHandler(value = MetException.class)
    @ResponseBody
    public ErrorInfo<String> jsonErrorHandler(HttpServletRequest req, MetException e) {
        ErrorInfo<String> r = new ErrorInfo<>();
        r.setMessage(e.getMessage());
        r.setCode(ErrorInfo.ERROR);
        r.setData("Some Data");
        r.setUrl(req.getRequestURL().toString());
        return r;
    }

}